const { ObjectID } = require('bson');
const dayjs = require('dayjs')
const db = require('./core')
// console.log('db', db);
module.exports = {
    async findOneUser(query) {
        const user = await db.findOne('user', query)
        user && delete user.password
        return user;
    },
    addOneUser(user) {
        return db.insertOne('user', user)
    },

    // 写与文章相关的
    // 添加一篇文章
    addOnePost(post) {
        return db.insertOne('post', post)
    },
    // 找到一篇文章
    async findOnePost(query) {
        const post = await db.findOne('post', query) //这里的post里面只包含了作者的id(authorId)
        // 找到文章作者的详细信息
        if (post) {
            const user = await module.exports.findOneUser({
                _id: post.authorId
            });
            post.author = user;
            post.created = dayjs(post._id.getTimestamp()).format('YYYY-MM-DD HH:mm:ss')
        }
        return post

    },

    // 找到多篇文章
    async findPost(query = {}, { limit = 10, skip = 0, sort = '-1' } = {}) {
        // 获取文章的总数量
        const count = await db.count('post');
        const posts = await db.findMany('post', query, limit, skip, sort)
        posts.forEach(post => {
            post.created = dayjs(post._id.getTimestamp()).format('YYYY-MM-DD HH:mm:ss')
        })

        return { // ~~ 按位(二进制位) 取反，~~两个波浪线  用于取整
            count, limit, skip, page: ~~(skip / limit), posts
        }
    },
    // 更新一篇文章
    async updateOnePost(query, data) {
        return db.updateOne('post', query, data)
    },
    async updateOneUser(query, data) {
        return db.updateOne('user', query, data)
    },
    // 查找作者
    async findUser(query = {}, { limit = 6, skip = 0, sort = 1 } = {}) {
        const users = await db.findMany('user', query, limit, skip, sort = { _id: sort });
        users.forEach(user => delete user.password)
        return users
    },
    // 添加评论
    async addComment(comment = { content: '', authorId: new ObjectID(), postId: new ObjectID() }) {
        const { insertedId: _id } = await db.insertOne('comment', comment)
        console.log('_id:', _id);
        return await module.exports.findOneComment({ _id })
    },
    // 查找评论
    async findOneComment(query = {}) {
        const result = await db.findOne('comment', query)
        // 查找 评论的作者的信息
        const author = await module.exports.findOneUser({ _id: result.authorId })
        result.author = author
        result.created = dayjs(result._id.getTimestamp()).format('YYYY-MM-DD HH:mm:ss')
        return result
    },
    // 根据当前评论的post，找到该post 的 所有评论信息
    async findCommentByPost(postId) {
        // 
        const comments = await db.findMany('comment', { postId })
        // 遍历comments数组 根据其中的 comment成员  查找对应的用户
        // 1.失败写法
        // 请注意：async 修饰过的函数 会变成异步函数，他的执行会放在同步任务之后执行
        // comments.forEach(async comment => {
        //     const author = await module.exports.findUser({ _id: comment.authorId })
        //     comment.author = author
        // });
        // 2.直接循环内部 await的话 每一次await都会让代码的执行停止，所以会造成 程序的阻塞，导致执行时间延长
        for (const comment of comments) {
            const author = await module.exports.findUser({ _id: comment.authorId })
            comment.author = author
        }
        return comments
    },




};
// 测试
async function test() {
    console.log(await module.exports.addComment({ content: 'qwe', authorId: new ObjectID(), postId: new ObjectID() }));

    console.log('*****', await module.exports.findCommentByPost(new ObjectID('6195f843fb371baff0375c07')));
};
// test()
